﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;

namespace WCSY.DAL
{
    public class fg_approveObjMstAppService
    {
        public fg_approveObjMstAppService()
		{}
		#region  Method


		/// <summary>
		/// 是否存在该记录
		/// </summary>
		public bool Exists(string id)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) from fg_approveObjMstApp");
			strSql.Append(" where id='"+id+"' ");
			return DBHelper.Exists(strSql.ToString());
		}

		/// <summary>
		/// 增加一条数据
		/// </summary>
		public string Add(WCSY.MODEL.fg_approveObjMstApp model)
		{
			StringBuilder strSql=new StringBuilder();
			StringBuilder strSql1=new StringBuilder();
			StringBuilder strSql2=new StringBuilder();
			if (model.id != null)
			{
				strSql1.Append("id,");
				strSql2.Append("'"+model.id+"',");
			}
			if (model.opNo != null)
			{
				strSql1.Append("opNo,");
				strSql2.Append("'"+model.opNo+"',");
			}
			if (model.billNo != null)
			{
				strSql1.Append("billNo,");
				strSql2.Append("'"+model.billNo+"',");
			}
			if (model.sendPsn != null)
			{
				strSql1.Append("sendPsn,");
				strSql2.Append("'"+model.sendPsn+"',");
			}
			if (model.sendDate != null)
			{
				strSql1.Append("sendDate,");
				strSql2.Append("'"+model.sendDate+"',");
			}
			if (model.endPsn != null)
			{
				strSql1.Append("endPsn,");
				strSql2.Append("'"+model.endPsn+"',");
			}
			if (model.endDate != null)
			{
				strSql1.Append("endDate,");
				strSql2.Append("'"+model.endDate+"',");
			}
			if (model.sendInfo != null)
			{
				strSql1.Append("sendInfo,");
				strSql2.Append("'"+model.sendInfo+"',");
			}
			if (model.remarks != null)
			{
				strSql1.Append("remarks,");
				strSql2.Append("'"+model.remarks+"',");
			}
			strSql.Append("insert into fg_approveObjMstApp(");
			strSql.Append(strSql1.ToString().Remove(strSql1.Length - 1));
			strSql.Append(")");
			strSql.Append(" values (");
			strSql.Append(strSql2.ToString().Remove(strSql2.Length - 1));
			strSql.Append(")");
			return strSql.ToString();
			
		}

		/// <summary>
		/// 更新一条数据
		/// </summary>
		public bool Update(WCSY.MODEL.fg_approveObjMstApp model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("update fg_approveObjMstApp set ");
			if (model.opNo != null)
			{
				strSql.Append("opNo='"+model.opNo+"',");
			}
			else
			{
				strSql.Append("opNo= null ,");
			}
			if (model.billNo != null)
			{
				strSql.Append("billNo='"+model.billNo+"',");
			}
			else
			{
				strSql.Append("billNo= null ,");
			}
			if (model.sendPsn != null)
			{
				strSql.Append("sendPsn='"+model.sendPsn+"',");
			}
			else
			{
				strSql.Append("sendPsn= null ,");
			}
			if (model.sendDate != null)
			{
				strSql.Append("sendDate='"+model.sendDate+"',");
			}
			else
			{
				strSql.Append("sendDate= null ,");
			}
			if (model.endPsn != null)
			{
				strSql.Append("endPsn='"+model.endPsn+"',");
			}
			else
			{
				strSql.Append("endPsn= null ,");
			}
			if (model.endDate != null)
			{
				strSql.Append("endDate='"+model.endDate+"',");
			}
			else
			{
				strSql.Append("endDate= null ,");
			}
			if (model.sendInfo != null)
			{
				strSql.Append("sendInfo='"+model.sendInfo+"',");
			}
			else
			{
				strSql.Append("sendInfo= null ,");
			}
			if (model.remarks != null)
			{
				strSql.Append("remarks='"+model.remarks+"',");
			}
			else
			{
				strSql.Append("remarks= null ,");
			}
			int n = strSql.ToString().LastIndexOf(",");
			strSql.Remove(n, 1);
			strSql.Append(" where id='"+ model.id+"' ");
			int rowsAffected=DBHelper.ExecuteSql(strSql.ToString());
			if (rowsAffected > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}

		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool Delete(string id)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from fg_approveObjMstApp ");
			strSql.Append(" where id='"+id+"' " );
			int rowsAffected=DBHelper.ExecuteSql(strSql.ToString());
			if (rowsAffected > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
        /// <summary>
        /// 根据单据号，删除审批步骤主表信息（重新送审时和取消审批时使用）
        /// </summary>
        /// <param name="billno"></param>
        /// <returns></returns>
        public string delApprovObjMstByBillno(string billno) {
            string sql = @" delete fg_approvobjmst where billno = '"+billno+"'";
            return sql;
        }
        /// <summary>
		/// 批量删除数据
		/// </summary>
		public bool DeleteList(string idlist )
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from fg_approveObjMstApp ");
			strSql.Append(" where id in ("+idlist + ")  ");
			int rows=DBHelper.ExecuteSql(strSql.ToString());
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public WCSY.MODEL.fg_approveObjMstApp GetModel(string id)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select  top 1  ");
			strSql.Append(" id,opNo,billNo,sendPsn,sendDate,endPsn,endDate,sendInfo,remarks ");
			strSql.Append(" from fg_approveObjMstApp ");
			strSql.Append(" where id='"+id+"' " );
			WCSY.MODEL.fg_approveObjMstApp model=new WCSY.MODEL.fg_approveObjMstApp();
			DataSet ds=DBHelper.Query(strSql.ToString());
			if(ds.Tables[0].Rows.Count>0)
			{
				if(ds.Tables[0].Rows[0]["id"]!=null && ds.Tables[0].Rows[0]["id"].ToString()!="")
				{
					model.id=ds.Tables[0].Rows[0]["id"].ToString();
				}
				if(ds.Tables[0].Rows[0]["opNo"]!=null && ds.Tables[0].Rows[0]["opNo"].ToString()!="")
				{
					model.opNo=ds.Tables[0].Rows[0]["opNo"].ToString();
				}
				if(ds.Tables[0].Rows[0]["billNo"]!=null && ds.Tables[0].Rows[0]["billNo"].ToString()!="")
				{
					model.billNo=ds.Tables[0].Rows[0]["billNo"].ToString();
				}
				if(ds.Tables[0].Rows[0]["sendPsn"]!=null && ds.Tables[0].Rows[0]["sendPsn"].ToString()!="")
				{
					model.sendPsn=ds.Tables[0].Rows[0]["sendPsn"].ToString();
				}
				if(ds.Tables[0].Rows[0]["sendDate"]!=null && ds.Tables[0].Rows[0]["sendDate"].ToString()!="")
				{
					model.sendDate=DateTime.Parse(ds.Tables[0].Rows[0]["sendDate"].ToString());
				}
				if(ds.Tables[0].Rows[0]["endPsn"]!=null && ds.Tables[0].Rows[0]["endPsn"].ToString()!="")
				{
					model.endPsn=ds.Tables[0].Rows[0]["endPsn"].ToString();
				}
				if(ds.Tables[0].Rows[0]["endDate"]!=null && ds.Tables[0].Rows[0]["endDate"].ToString()!="")
				{
					model.endDate=DateTime.Parse(ds.Tables[0].Rows[0]["endDate"].ToString());
				}
				if(ds.Tables[0].Rows[0]["sendInfo"]!=null && ds.Tables[0].Rows[0]["sendInfo"].ToString()!="")
				{
					model.sendInfo=ds.Tables[0].Rows[0]["sendInfo"].ToString();
				}
				if(ds.Tables[0].Rows[0]["remarks"]!=null && ds.Tables[0].Rows[0]["remarks"].ToString()!="")
				{
					model.remarks=ds.Tables[0].Rows[0]["remarks"].ToString();
				}
				return model;
			}
			else
			{
				return null;
			}
		}
		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetList(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select id,opNo,billNo,sendPsn,sendDate,endPsn,endDate,sendInfo,remarks ");
			strSql.Append(" FROM fg_approveObjMstApp ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			return DBHelper.Query(strSql.ToString());
		}

		/// <summary>
		/// 获得前几行数据
		/// </summary>
		public DataSet GetList(int Top,string strWhere,string filedOrder)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select ");
			if(Top>0)
			{
				strSql.Append(" top "+Top.ToString());
			}
			strSql.Append(" id,opNo,billNo,sendPsn,sendDate,endPsn,endDate,sendInfo,remarks ");
			strSql.Append(" FROM fg_approveObjMstApp ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			strSql.Append(" order by " + filedOrder);
			return DBHelper.Query(strSql.ToString());
		}

		/// <summary>
		/// 获取记录总数
		/// </summary>
		public int GetRecordCount(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) FROM fg_approveObjMstApp ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			object obj = DBHelper.GetSingle(strSql.ToString());
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("SELECT * FROM ( ");
			strSql.Append(" SELECT ROW_NUMBER() OVER (");
			if (!string.IsNullOrEmpty(orderby.Trim()))
			{
				strSql.Append("order by T." + orderby );
			}
			else
			{
				strSql.Append("order by T.id desc");
			}
			strSql.Append(")AS Row, T.*  from fg_approveObjMstApp T ");
			if (!string.IsNullOrEmpty(strWhere.Trim()))
			{
				strSql.Append(" WHERE " + strWhere);
			}
			strSql.Append(" ) TT");
			strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
			return DBHelper.Query(strSql.ToString());
		}

		/*
		*/

		#endregion  Method
    }
}
